# -*- coding: utf-8 -*-
"""
Created on Mon Dec  4 14:41:29 2023

@author: 李詩婷-21551120028
classfier.score(features_test, target_test)=0.9273449438709386
dummy.score(features_test, target_test)=0.5010929208390021
roc_auc_score(target_test,target_probabilities)=0.5372731625488096
cv_results.mean()=0.858860062096365
"""
# 导入相关库
from sklearn.model_selection import train_test_split
from sklearn import datasets
from sklearn import metrics
from sklearn.model_selection import KFold, cross_val_score
from sklearn.pipeline import make_pipeline
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import StandardScaler
import pandas as pd
# 导入数据

# 进行标准化
stand = StandardScaler()
# 创建logistic回归器
logistic = LogisticRegression()
# 创建一个包含数据标准化和逻辑回归的流水线
pipline = make_pipeline(stand, logistic)# 先对数据进行标准化，再用logistic回归拟合
# 创建k折交叉验证对象
kf = KFold(n_splits=100, shuffle=True, random_state=1)

data1=pd.read_csv(r'F:\大三实验作业\Python数据分析与应用实验报告\dataset\\train_df.csv')
train=pd.DataFrame(data1.drop(['udmap','target'],axis=1))
target=data1['target']
test1=pd.read_csv(r'F:\大三实验作业\Python数据分析与应用实验报告\dataset\\test_df.csv')
test=pd.DataFrame(test1.drop(['udmap'],axis=1))
#test=pd.read_csv(r'F:\大三实验作业\Python数据分析与应用实验报告\dataset\\test.csv')
from sklearn.model_selection import train_test_split
#data_train,  data_test,target_train,target_test = train_test_split(train, test,test_size=0.2,random_state=1)

stand.fit(train)
StandardScaler()
# 然后在训练集和测试集上运用
data_train_std = stand.transform(train)
data_test_std = stand.transform(test)
pipeline = make_pipeline(stand, logistic)
cv_results = cross_val_score(pipline,
                            data_train_std,
                            target,
                            cv=kf,
                            scoring='accuracy',
                            n_jobs=-1)
cv_results.mean()
from sklearn.datasets import load_boston
from sklearn.dummy import DummyRegressor
from sklearn.model_selection import train_test_split
# 将数据分为测试集和训练集
features_train, features_test, target_train, target_test = train_test_split(train,target,test_size=0.2,random_state=0)


# 创建dummyregression对象
dummy = DummyRegressor(strategy='mean')

# 训练模型
# 训练模型
dummy.fit(features_train, target_train)
DummyRegressor()
dummy.score(features_test, target_test)


# 下面我们训练自己的模型进行对比
from sklearn.linear_model import LinearRegression
ols = LinearRegression()
ols.fit(features_train, target_train)

LinearRegression()
ols.score(features_test, target_test)

# 导入相关库
import matplotlib.pyplot as plt
from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import roc_curve, roc_auc_score
from sklearn.model_selection import train_test_split

# 创建逻辑回归器
logit = LogisticRegression()


features_train, features_test, target_train, target_test = train_test_split(train,
                                                                            target,
                                                                           	test_size=.1,
                                                                           	random_state=1)



logit.fit(features_train, target_train)
LogisticRegression()
# 预测为1的概率
target_probabilities = logit.predict_proba(features_test)[:,1]
target_test

false_positive_rate, true_positive_rate, thresholds = roc_curve(target_test,target_probabilities)

y_predict = target_probabilities>0.6
y_predict

# 绘制AUC曲线
plt.plot(false_positive_rate, true_positive_rate)
plt.plot([0, 1], ls='--')
plt.plot([0, 0], [1, 0], c='.7')
plt.plot([1,1], c='.7')

# 我们可以通过predict_proba 查看样本的预测概率
logit.predict_proba(features_test)[2]

logit.classes_

roc_auc_score(target_test,target_probabilities)

from sklearn.datasets import load_iris
from sklearn.dummy import DummyClassifier
from sklearn.model_selection import train_test_split

# 划分数据集
features_train, features_test, target_train, target_test = train_test_split(train, target,
                                                                           random_state=0)

dummy = DummyClassifier(strategy='uniform', random_state=1)

dummy.fit(features_train, target_train)

DummyClassifier(random_state=1, strategy='uniform')

dummy.score(features_test, target_test)

from sklearn.ensemble import RandomForestClassifier#随机森林分类，考虑在后面分享

classfier = RandomForestClassifier()

classfier.fit(features_train, target_train)

RandomForestClassifier()
classfier.score(features_test, target_test)




















